<?php

namespace app\admin\controller;

use think\Request;
use app\common\server\Code;
use app\common\model\Tag as TagModel;

class Tag extends Base
{
   public function lst(){
       $list = TagModel::getList();
       $this->assign("list",$list);
       return $this->fetch();
   }

    public function add(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $validateResult = $this->validateData('tag', 'add');
            if ($validateResult !== true) return apiReturn(Code::$error, $validateResult);
            $result = TagModel::create($params);
            if ($result) return apiReturn();
            return apiReturn(Code::$error);
        }else{
            return $this->fetch();
        }
    }

    public function edit(Request $request,$id){
        if($request->isPost()){
            $params = $request->param();
            $validateResult=$this->validateData('tag','edit',$params);
            if($validateResult!==true) return apiReturn(Code::$error,$validateResult);

            $info = TagModel::get($id);
            if(!$info) return apiReturn(Code::$error);
            $info->tag_name = $params['tag_name'];
            $info->save();
            return apiReturn();
        }else{
            $info =TagModel::get($id);
            $this->assign("info",$info);
            return $this->fetch();
        }
    }

    public function delete($id){
        if(TagModel::destroy($id)) return apiReturn();
        return apiReturn(Code::$error,"删除失败！");
    }
}
